package com.atguigu.boot3.ssm.controller;

import com.atguigu.boot3.ssm.bean.User;
import com.atguigu.boot3.ssm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-04-20 17:07
 */
@RestController
@RequestMapping("/sanda/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

//    /**
//     * 返回User的json数据
//     * @param id
//     * @return
//     */
//    @GetMapping("/user/{id}")
//    public TUser getUser(@PathVariable("id") Long id){
//        TUser user = userMapper.getUserById(id);
//        return user;
//    }


    @GetMapping(value = "/findall")
    public List<User> findAll(){
        return userMapper.findAll();
    }

    @GetMapping(value = "/findById")
    public List<User> findById(@PathVariable Integer id){
        return userMapper.findById(id);
    }

    //增加和修改
    @PostMapping(value = "/save")
    public int save(@RequestBody User user){
        if(user.getId()==null){//若无id号则插入新数据
            return userMapper.insert(user);
        }else{//若有id号则修改
            return userMapper.update(user);
        }
    }

    //删除
    @GetMapping(value="/del/{id}")
    public Integer Delect(@PathVariable Integer id){
        return userMapper.delectById(id);
    }

}
